package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Rule;
import com.foreknow.entity.RuleMap;
import com.foreknow.service.RuleService;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rule")
public class RuleController {
    @Autowired
    private RuleService ruleService;
    @RequestMapping(value = "/finRuleByPage", method = RequestMethod.GET)
    public String findRuleByPage(@RequestParam Map<String, String> para) throws JsonProcessingException {
        //获取参数
        Integer deptId=null;
        Integer registleveId=null;
        if(!para.get("deptId").equals("") && !para.get("registleveId").equals("")) {
             deptId = Integer.parseInt(para.get("deptId"));
             registleveId = Integer.parseInt(para.get("registleveId"));
        }
        Integer pageNum = Integer.parseInt(para.get("pageNum"));
        Integer pageSize = Integer.parseInt(para.get("pageSize"));
        HashMap<String, Object> hs = new HashMap<>();
        //设置分页信息
        PageHelper.startPage(pageNum, pageSize);
        List<RuleMap> ruleMapList = ruleService.queryRuleByQuery(deptId,registleveId);
        PageInfo<RuleMap> info = new PageInfo<>(ruleMapList);
        //给map添加数据
        hs.put("totalpage", info.getTotal());
        hs.put("pagenum", info.getPageNum());
        hs.put("rules", ruleMapList);
        if (ruleMapList.size() > 0) {//如果获取到数据
            hs.put("meta", new Constance("获取成功", 200));
        } else {
            hs.put("meta", new Constance("没有查询到数据", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/addRules" , method = RequestMethod.POST)
    public String addRules(@RequestBody Map<String,Object>para) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        ObjectMapper objectMapper = new ObjectMapper();
        String ruleName=String.valueOf(para.get("ruleName"));
        List<Object> rulesObj = (List<Object>) para.get("rules");
        List<Rule> rules = new ArrayList<>();
        boolean isRight = false;
        if (rulesObj.size()>0 && !ruleName.equals("")) {
            for (Object rule : rulesObj) {
                //设置在反序列化时忽略在JSON字符串中存在，而在Java中不存在的属性
                Rule ruleItem = objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).convertValue(rule, Rule.class);
                ruleItem.setRulename(ruleName);
                rules.add(ruleItem);
            }
            isRight = ruleService.addRules(rules);
        }
        if (isRight){
            hs.put("meta", new Constance("添加成功", 200));
        }else{
            hs.put("meta", new Constance("添加失败", 401));
        }
        return objectMapper.writeValueAsString(hs);
    }

}


